package com.jushu.storbox.entities;

import android.content.ContentValues;
import android.database.Cursor;

import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.database.IBaseDao;
import com.jushu.storbox.database.IDownloadFileDao;
import com.jushu.storbox.database.IObjectConvert;
import com.jushu.storbox.task.tqueue.DownloadTask;
import com.jushu.storbox.util.PublicTools;
import com.huawei.sharedrive.sdk.android.modelV2.response.FileInfoResponseV2;

public class DownloadObject extends ResourceEntity implements IObjectConvert{

	/** *  */
	private static final long serialVersionUID = -5543437448257791697L;
	
	private String fileName = "";
	private long fileSize = 0;
	private String parentId = "";
	
	private String objectId;
	
	private long currentPaogress = 0;
	private long currentSize = 0;
	
	private long lastUpdateTime = 0;
	private int transStatus = Constant.TRANSLATE_IS_NOTHING;
	private int isSync = IDownloadFileDao.REQUEST_SYNC_FALSE;
	private String currentUserId = null;
	
	private transient DownloadTask downloadTask = null;
	
	public String getCurrentUserId() {
		return currentUserId;
	}

	public void setCurrentUserId(String currentUserId) {
		this.currentUserId = currentUserId;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public long getFileSize() {
		return fileSize;
	}

	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public long getCurrentPaogress() {
		return currentPaogress;
	}

	public void setCurrentPaogress(long currentPaogress) {
		this.currentPaogress = currentPaogress;
	}

	public long getCurrentSize() {
		return currentSize;
	}

	public void setCurrentSize(long currentSize) {
		this.currentSize = currentSize;
	}

	public long getLastUpdateTime() {
		return lastUpdateTime;
	}

	public void setLastUpdateTime(long lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	}

	public int getTransStatus() {
		return transStatus;
	}

	public void setTransStatus(int transStatus) {
		this.transStatus = transStatus;
	}
	
	public String getObjectId() {
		return objectId;
	}

	public void setObjectId(String objectId) {
		this.objectId = objectId;
	}

	public boolean isSync() {
		return isSync == IDownloadFileDao.REQUEST_SYNC_TRUE;
	}

	public void setSync(boolean syncFlag) {
		if(syncFlag) {
			this.isSync = IDownloadFileDao.REQUEST_SYNC_TRUE;
		} else {
			this.isSync = IDownloadFileDao.REQUEST_SYNC_FALSE;
		}
	}
	
	private void setSync(int syncFlag) {
		switch(syncFlag) {
		case IDownloadFileDao.REQUEST_SYNC_TRUE:
			isSync = IDownloadFileDao.REQUEST_SYNC_TRUE;
			break;
		case IDownloadFileDao.REQUEST_SYNC_FALSE:
			isSync = IDownloadFileDao.REQUEST_SYNC_FALSE;
			break;
		default:
			isSync = IDownloadFileDao.REQUEST_SYNC_FALSE;
		}
	}

	public DownloadTask getDownloadTask() {
		return downloadTask;
	}

	public void setDownloadTask(DownloadTask downloadTask) {
		this.downloadTask = downloadTask;
	}

	public ContentValues toContentValues() {
		ContentValues cv = new ContentValues();
		cv.put(IBaseDao.id, getId());
		cv.put(IBaseDao.owner_by, getOwnerBy());
		cv.put(IBaseDao.file_name, getFileName());
		cv.put(IBaseDao.parent_folder_id, getParentId());
//		cv.put(IBaseDao.trans_status, Constant.TRANSLATE_IS_NOTHING);     
		cv.put(IBaseDao.trans_status, getTransStatus());
		cv.put(IBaseDao.current_progress, getCurrentPaogress()); 
		cv.put(IBaseDao.current_size, getCurrentSize());     
		cv.put(IBaseDao.file_size, getFileSize());
		cv.put(IBaseDao.lastUpdateDate, PublicTools.getCurrentTime()); 
		cv.put(IBaseDao.isSync, isSync); 
		cv.put(IDownloadFileDao.current_user_id, getCurrentUserId());
		return cv;
	}
	
	public void fromCursor(Cursor cursor) {
		setId(cursor.getString(cursor.getColumnIndex(IBaseDao.id)));
		setOwnerBy(cursor.getString(cursor.getColumnIndex(IBaseDao.owner_by)));
		setFileName(cursor.getString(cursor.getColumnIndex(IBaseDao.file_name)));
		setParentId(cursor.getString(cursor.getColumnIndex(IBaseDao.parent_folder_id)));
		setTransStatus(cursor.getInt(cursor.getColumnIndex(IBaseDao.trans_status)));
		setCurrentPaogress(cursor.getLong(cursor.getColumnIndex(IBaseDao.current_progress)));
		setCurrentSize(cursor.getLong(cursor.getColumnIndex(IBaseDao.current_size)));
		setFileSize(cursor.getLong(cursor.getColumnIndex(IBaseDao.file_size)));
		setLastUpdateTime(cursor.getLong(cursor.getColumnIndex(IBaseDao.lastUpdateDate)));
		setSync(cursor.getInt(cursor.getColumnIndex(IBaseDao.isSync)));
		setCurrentUserId(cursor.getString(cursor.getColumnIndex(IDownloadFileDao.current_user_id)));
	}
	
	public void fromFileFolderInfo(FileFolderInfo fileFolderInfomation) {
		if(fileFolderInfomation == null || fileFolderInfomation instanceof FolderInfo) {
			throw new IllegalArgumentException(" the fileFolder instance is null or is FolderInfo");
		}
		setId(fileFolderInfomation.getId());
		setOwnerBy(fileFolderInfomation.getOwnerBy());
		setFileName(fileFolderInfomation.getName());
		setParentId(fileFolderInfomation.getParent());
		setTransStatus(fileFolderInfomation.getTransStatus());
		setCurrentPaogress(0);
		setCurrentSize(0);
		setFileSize(fileFolderInfomation.getSize());
		setLastUpdateTime(PublicTools.getCurrentTime());
		setObjectId(fileFolderInfomation.getObjectID());
		setSync(fileFolderInfomation.isSync());
		setCurrentUserId(fileFolderInfomation.getCurrentUserId());
	}
	
	public void fromServerFileInfo(FileInfoResponseV2 serverNodeInfo) {
		if(serverNodeInfo == null) {
			throw new IllegalArgumentException(" the fileFolder instance is null or is not FileInfo");
		}
		setId(serverNodeInfo.getId());
		setOwnerBy(serverNodeInfo.getOwnerBy());
		setFileName(serverNodeInfo.getName());
		setParentId(serverNodeInfo.getParent());
		setTransStatus(Constant.TRANSLATE_IS_NOTHING);
		setCurrentPaogress(0);
		setCurrentSize(0);
		setFileSize(serverNodeInfo.getSize());
		setLastUpdateTime(PublicTools.getCurrentTime());
		setObjectId("");
		setSync(false);
		setCurrentUserId(ShareDriveApplication.getInstance().getWnerID());
	}
	
}
